package com.ft.oa.service.customer.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ft.oa.domain.customer.Customer;
import com.ft.oa.domain.customer.CustomerSynergy;
import com.ft.oa.enums.ManagerEnum;
import com.ft.oa.mapper.customer.CustomerMapper;
import com.ft.oa.mapper.customer.CustomerSynergyMapper;
import com.ft.oa.service.customer.CustomerSynergyService;
import com.ft.oa.utils.SecurityUtils;
import com.ft.oa.web.customer.rq.CustomerSynergyPage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class CustomerSynergyServiceImpl implements CustomerSynergyService {


    @Resource
    private CustomerSynergyMapper customerSynergyMapper;
    @Resource
    private CustomerMapper customerMapper;


    /**
     * 查询协同列表
     * @param customerSynergyPage
     * @return
     */
    @Override
    public IPage<CustomerSynergy> selectConditionCustomerSynergyList(CustomerSynergyPage customerSynergyPage) {
        IPage<CustomerSynergy> page = new Page<>(customerSynergyPage.getPage(),customerSynergyPage.getSize());
        QueryWrapper<CustomerSynergy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("data_status",0);
        if (customerSynergyPage.getSynergyProject()!=null){
            queryWrapper.like("synergy_project",customerSynergyPage.getSynergyProject());
        }
        if (customerSynergyPage.getName()!=null&&!"".equals(customerSynergyPage.getName())){
            queryWrapper.like("name",customerSynergyPage.getName());
        }
        if(ManagerEnum.GENERAL.toString().equals(SecurityUtils.getCurrentUser().getManager())){
            queryWrapper.eq("creator",SecurityUtils.getCurrentUser().getId());
        }
        queryWrapper.select().orderByDesc("synergy_date");
        IPage<CustomerSynergy> customerSynergyIPage = customerSynergyMapper.selectPage(page, queryWrapper);
        customerSynergyIPage.getRecords().forEach(
                e->{
                    if( 1 ==  e.getStatus()){
                        e.setSynergyStatus("成功");
                    }
                    if( 2 ==  e.getStatus()){
                        e.setSynergyStatus("失败");
                    }
                    if( 3 ==  e.getStatus()){
                        e.setSynergyStatus("搁置");
                    }
                }
        );
        return customerSynergyIPage;
    }

    /**
     * 添加客户协同
     * @param customerSynergy
     * @return
     */
    @Override
    public int insertCustomerSynergy(CustomerSynergy customerSynergy) {
       if (customerSynergy != null) {
           Customer customer = customerMapper.selectById(customerSynergy.getCustomerId());
           customerSynergy.setName( customer.getName());
           customerSynergy.setDataStatus(0);
           return customerSynergyMapper.insert(customerSynergy);
       }else {
           return 0;
       }
    }

    /**
     * 修改
     * @param customerSynergy
     * @return
     */
    @Override
    public int updateCustomerSynergy(CustomerSynergy customerSynergy) {
        if (customerSynergy != null) {
            Customer customer = customerMapper.selectById(customerSynergy.getCustomerId());
            customerSynergy.setName( customer.getName());
            return customerSynergyMapper.updateById(customerSynergy);
        }else {
            return 0;
        }
    }

    /**
     * 删除
     * @return
     */
    @Override

    public int deleteCustomerSynergy(CustomerSynergy customerSynergy) {
        CustomerSynergy synergy = customerSynergyMapper.selectById(customerSynergy.getId());
        if (synergy.getDataStatus() ==0){
            synergy.setDataStatus(1);
            customerSynergyMapper.updateById(synergy);
        }
        return 0;
    }

    /**
     * 根据ID查询公司信息
     *
     * @param customerSynergy
     * @return
     */
    @Override
    public CustomerSynergy selectById(CustomerSynergy customerSynergy) {
       return customerSynergyMapper.selectById(customerSynergy.getId());
    }
}
